﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web.Mvc;
using System.Text;
using System.EnterpriseServices;
using Webdiyer.WebControls.Mvc;
using YiYiTong.Rocket.Business;
using YiYiTong.Rocket.Entity;
using YiYiTong.Rocket.Model.Manager;
using YiYiTong.Rocket.Helper;
using YiYiTong.Rocket.Model.ParamModel;
using YiYiTong.Rocket.EFDBFirst;
using System.Web;
using System.IO;
using System.Text.RegularExpressions;
using YiYiTong.Rocket.Model;

namespace YiYiTong.Rocket.ManageWeb.Controllers
{
    public class dayinController : Controller
    {
        [NoVerify]
        public ActionResult header()
        {
            return View();
        }
        [NoVerify]
        public ActionResult Pdf(string print_type, int id)
        {
            //        string header = @"    <table class='definewidth m10'>
            //    <tr>
            //        <td style='width:170px'>
            //            <img src='~/Images/logo.png' width='170' />
            //        </td>
            //        <td style='font-size:18px;line-height:24px;padding-left:20px;'>
            //            Suzhou Yiyitong E-commerce Co.,Ltd.<br />
            //            苏州易易通电子商务有限公司<br />
            //            http://www.ezezic.com/<br />
            //        </td>
            //    </tr>
            //</table>";
            string url = Request.Url.ToString();
            url = url.Substring(0, url.ToLower().IndexOf("/dayin"));
            string headerurl = url + "/dayin/header";
            string paramss = $"--print-media-type --header-line --margin-top 25mm --margin-bottom 2cm --header-spacing 5 --footer-spacing 3 --footer-line --footer-right [page]/[topage]  --header-html {headerurl}";
            switch (print_type)
            {
                case "fahuotongzhi":
                    return new Rotativa.ActionAsPdf("fahuotongzhi", new { id = id })
                    {
                        PageSize = Rotativa.Options.Size.A4,

                        FileName = $"发货通知-{id}.pdf",
                        CustomSwitches = paramss //"--print-media-type --header-line –header-spacing 10 --footer-line --footer-center [page]/[topage] --header-html http://localhost:8079/dayin/header",


                    };
                case "caigoudan":
                    return new Rotativa.ActionAsPdf("caigoudan", new { id = id })
                    {
                        PageSize = Rotativa.Options.Size.A4,
                        CustomSwitches = paramss,
                        FileName = $"采购单-{id}.pdf",
                    };
                case "xiaoshouhetong":
                    return new Rotativa.ActionAsPdf("xiaoshouhetong", new { id = id })
                    {
                        PageSize = Rotativa.Options.Size.A4,
                        CustomSwitches = paramss,
                        FileName = $"销售合同-{id}.pdf",
                    };
            }

            return View();

            //if(print_type == "")
            //return new Rotativa.ActionAsPdf("fahuotongzhi", new { id = id }) { FileName = $"发货通知-{id}.pdf" };
            //var rot = new Rotativa.UrlAsPdf("http://localhost:8079/dayin/fahuotongzhi/453");
            //rot.PageSize = Rotativa.Options.Size.A4;
            //return rot;
            //return View();
        }

        private OrderMasterModel GetModel(long id)
        {
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            OrderMasterModel paramModel = new OrderMasterModel();
            if (id > 0)
            {
                paramModel.view_Order = dbcontext.View_Order.FirstOrDefault(t => t.order_id == id);

                paramModel.aDZ_MemberMaster = dbcontext.ADZ_MemberMaster.FirstOrDefault(t => t.uid == paramModel.view_Order.uid);

                if (paramModel.view_Order.is_invoice.Value == 1)
                {
                    //发票信息
                    var aDZ_OrderInvoiceRel = dbcontext.ADZ_OrderInvoiceRel.FirstOrDefault(t => t.order_id == id);
                    if (aDZ_OrderInvoiceRel != null)
                    {
                        paramModel.aDZ_OrderInvoiceRel = aDZ_OrderInvoiceRel;
                    }
                }

                //收货信息
                paramModel.aDZ_OrderAddress = dbcontext.ADZ_OrderAddress.FirstOrDefault(t => t.order_id == id);
                paramModel.aDZ_OrderAddress = paramModel.aDZ_OrderAddress ?? new ADZ_OrderAddress();

                var express = dbcontext.ADZ_ExpressConfig.FirstOrDefault(t => t.express_com == paramModel.aDZ_OrderAddress.express_com);
                if (express != null)
                    paramModel.aDZ_OrderAddress.express_com = express.express_name;

                //商品列表
                var aDZ_OrderDetails = dbcontext.ADZ_OrderDetails.Where(t => t.order_id == id).ToList();
                foreach (var item in aDZ_OrderDetails)
                {
                    paramModel.orderGoods.Add(new OrderGoods()
                    {
                        views_Goods = dbcontext.Views_Goods.FirstOrDefault(t => t.goods_id == item.goods_id),
                        aDZ_OrderDetails = item
                    });
                }
            }
            return paramModel;
        }

        [Description("打印发货通知")]
        [NoVerify]
        public ActionResult fahuotongzhi(int id)
        {
            ViewBag.Title = "打印发货通知";
            OrderMasterModel paramModel = GetModel(id);


            return View(paramModel);

        }

        [Description("打印采购单")]
        [NoVerify]
        public ActionResult caigoudan(int id)
        {
            OrderMasterModel paramModel = GetModel(id);
            ViewBag.Title = "打印采购单";

            return View(paramModel);

        }

        [Description("打印销售合同")]
        [NoVerify]
        public ActionResult xiaoshouhetong(int id)
        {
            OrderMasterModel paramModel = GetModel(id);
            ViewBag.Title = "打印销售合同";

            return View(paramModel);

        }


        [NoVerify]
        public ActionResult chukudan(int id)
        {
            OrderMasterModel paramModel = GetModel(id);
            ViewBag.Title = "打印出库单";

            string strOutPut;
            string Excel = ChukuExcel(paramModel, out strOutPut);
            ResponseMsg resMsg = new ResponseMsg();

            if (strOutPut != "导出成功")
            {
                resMsg.IsSuccess = false;
                resMsg.Message = strOutPut;

                return Json(resMsg, JsonRequestBehavior.AllowGet);
            }
            byte[] fileContents = Encoding.UTF8.GetBytes(Excel.ToString());
            return File(fileContents, "application/ms-excel", $"出库单-{id}.xls");

        }
        [NoVerify]
        public ActionResult rukudan(int id)
        {
            OrderMasterModel paramModel = GetModel(id);
            ViewBag.Title = "打印入库单";

            string strOutPut;
            string Excel = ChukuExcel(paramModel, out strOutPut);
            ResponseMsg resMsg = new ResponseMsg();

            if (strOutPut != "导出成功")
            {
                resMsg.IsSuccess = false;
                resMsg.Message = strOutPut;

                return Json(resMsg, JsonRequestBehavior.AllowGet);
            }
            byte[] fileContents = Encoding.UTF8.GetBytes(Excel.ToString());
            return File(fileContents, "application/ms-excel", $"入库单-{id}.xls");

        }
        private string ChukuExcel(OrderMasterModel paramModel, out string strOutPut)
        {
            List<string> list_title = new List<string> {
                "序号","商品编号","商品类别","料号","品牌","仓库全名","单位","数量","单据备注","状态"
            };
            ExportExcel Excel = new ExportExcel();
            DataTable dt = new DataTable();
            list_title.ForEach(t => dt.Columns.Add(t));
            dt.Columns["序号"].DataType = typeof(StringNumber);
            dt.Columns["商品编号"].DataType = typeof(StringNumber);
            dt.Columns["商品类别"].DataType = typeof(StringNumber);
            dt.Columns["料号"].DataType = typeof(StringNumber);
            dt.Columns["品牌"].DataType = typeof(StringNumber);
            dt.Columns["仓库全名"].DataType = typeof(StringNumber);
            dt.Columns["单位"].DataType = typeof(StringNumber);
            dt.Columns["数量"].DataType = typeof(StringNumber);
            dt.Columns["单据备注"].DataType = typeof(StringNumber);
            dt.Columns["状态"].DataType = typeof(StringNumber);
            if (!paramModel.orderGoods.Any())
            {
                return Excel.DtToExcel(dt, list_title, out strOutPut);
            }
            int i = 0;
            foreach (var item in paramModel.orderGoods)
            {
                DataRow dr = dt.NewRow();
                dr["序号"] = StringNumber.Crt(i++);
                dr["商品编号"] = StringNumber.Crt(item.views_Goods.goods_id);

                dr["商品类别"] = StringNumber.Crt(item.views_Goods.category_name);
                dr["料号"] = StringNumber.Crt(item.views_Goods.MPN);
                dr["品牌"] = StringNumber.Crt(item.views_Goods.brand_name);
                dr["仓库全名"] = StringNumber.Crt("原材料仓库");
                dr["单位"] = StringNumber.Crt(item.views_Goods.goods_unit);
                dr["数量"] = StringNumber.Crt(item.aDZ_OrderDetails.amount);
                dr["单据备注"] = StringNumber.Crt("无");
                dr["状态"] = StringNumber.Crt("");
                dt.Rows.Add(dr);
            }


            return Excel.DtToExcel(dt, list_title, out strOutPut);
        }
    }
}